// pages/refund/refund.js
const api = require('../../utils/http.js');
const app = getApp();
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		wayIndex: '',
		reasonIndex: '',
		tempFilePaths: [],
		arr: [],
		wayArray: [],
		reasonArray: [],
		money: '',
		complete: false,
		orderId: '',
		imgUrl: []
	},
	bindPickerChange(e) {
		this.setData({
			wayIndex: e.detail.value
		})
		// if (this.data.reasonIndex && this.data.tempFilePaths.length > 0) {
		// 	this.setData({
		// 		complete: true
		// 	})
		// }
    if (this.data.reasonIndex) {
      this.setData({
        complete: true
      })
    }
	},
	bindPickerReasonChange(e) {
		this.setData({
			reasonIndex: e.detail.value
		})
		// if (this.data.wayIndex && this.data.tempFilePaths.length > 0) {
		// 	this.setData({
		// 		complete: true
		// 	})
		// }
    if (this.data.wayIndex) {
      this.setData({
        complete: true
      })
    }
	},
	//退款金额输入
	handleTui(e) {
		console.log(e.detail.value);
		this.setData({
			money1: e.detail.value
		})
		if (e.detail.value > this.data.money) {
			this.setData({
				money1: this.data.money
			})
		}
	},
	//上传图片
	uploadIdentity(e) {
		wx.chooseImage({
			count: 1,
			sizeType: ['original', 'compressed'],
			sourceType: ['camera', 'album'],
			success: (res) => {
				let tempFilePaths = res.tempFilePaths
				wx.uploadFile({
					url: `${app.globalData.host}file`,
					filePath: tempFilePaths[0],
					name: 'file',
					success: (res) => {
						let data = JSON.parse(res.data)
						if (data.code == 1) {
							this.data.tempFilePaths.push(data.url)
							this.setData({
								tempFilePaths: this.data.tempFilePaths,
							})
							// if (this.data.wayIndex && this.data.reasonIndex) {
							// 	this.setData({
							// 		complete: true
							// 	})
							// }
						}
					}
				})
			}
		})
	},
	// 申请退款
	refundFormSubmit(e) {
		let refundInfo = e.detail.value
		if (!refundInfo.methods.trim()) {
			wx.showToast({
				title: '请选择处理方式',
				icon: 'none'
			})
		} else if (!refundInfo.reason.trim()) {
			wx.showToast({
				title: '请选择退款原因',
				icon: 'none'
			})
		} 
    // else if (this.data.tempFilePaths.length == 0) {
		// 	wx.showToast({
		// 		title: '请上传凭证',
		// 		icon: 'none'
		// 	})
		// } 
    else {
			this.setData({
				complete: true
			})

			if (this.data.refundInfo1) {
				this.modifyApply(refundInfo)
			} else {
				this.submitAjax(refundInfo)
			}
		}
	},
	// ajax提交
	submitAjax(refundInfo) {
		console.log(refundInfo)
		let reason = this.data.reasonArray[this.data.reasonIndex].id
		let methods = this.data.wayArray[this.data.wayIndex].id
		let imgArr = this.data.tempFilePaths.join(',')
		let uid = app.globalData.uid;
		api.fetchApi('procedure_t', {
			p: `R2003002|${this.data.orderId}|${methods}|${reason}|${refundInfo.explain}|${this.data.money}|${imgArr}`
		}).then(res => {
			if (res.data.code == 1) {
				wx.showToast({
					title: '申请退款成功',
					// icon: 'none',
					success: () => {
						setTimeout(() => {
							var pages = getCurrentPages();
							//上一个页面实例对象
							var prePage = pages[pages.length - 2];
							prePage.getOrderDetails();
							wx.navigateBack({
								delta: 1
							})
						}, 500)
					}
				})

			} else {
				wx.showToast({
					title: `${res.data.errorMsg}`,
					icon: 'none'
				})
			}
		})
	},
	wayArrayAjax() {
		api.fetchApi('procedure_t', {
			p: `R2007003|clfs`
		}).then(res => {
			const {
				code,
				results
			} = res.data
			if (code == 1) {
				if (this.data.refundInfo1) {
					var wayIndex = results.findIndex(item => item.id == this.data.refundInfo1.return_type);
				}
				this.setData({
					wayArray: results,
					wayIndex
				})
			} else {
				wx.showToast({
					title: `${res.data.errorMsg}`,
					icon: 'none'
				})
			}
		})
	},
	reasonArrayAjax() {
		api.fetchApi('procedure_t', {
			p: `R2007003|tkyy`
		}).then(res => {
			const {
				code,
				results
			} = res.data
			if (code == 1) {
				if (this.data.refundInfo1) {
					var reasonIndex = results.findIndex(item => item.id == this.data.refundInfo1.return_reason);
				}
				this.setData({
					reasonArray: results,
					reasonIndex
				})
			} else {
				wx.showToast({
					title: `${res.data.errorMsg}`,
					icon: 'none'
				})
			}
		})
	},
	getMoneyAjax() {
		api.fetchApi('procedure_t', {
			p: `R2007004|${this.data.orderId}`
		}).then(res => {
			const {
				code,
				results
			} = res.data
			if (code == 1) {
				this.setData({
					money: results[0].je,
					money1: results[0].je
				})
			} else {
				wx.showToast({
					title: `${res.data.errorMsg}`,
					icon: 'none'
				})
			}
		})
	},
	//修改申请
	modifyApply(refundInfo) {
		var refundInfo1 = this.data.refundInfo1;
		let reason = this.data.reasonArray[this.data.reasonIndex].id
		let methods = this.data.wayArray[this.data.wayIndex].id
		let imgArr = this.data.tempFilePaths.join(',')
    api.fetchApi('p_order_sqtk', {
			p: `R2007006|${this.data.orderId}|${methods}|${reason}|${refundInfo.explain}|${this.data.money1}|${imgArr}`
		}).then(res => {
			if (res.data.code == 1) {
				api.showToast('修改成功!' + '', '', '').then(() => {
					var pages = getCurrentPages();
					//上一个页面实例对象
					var prePage = pages[pages.length - 2];
					prePage.refundDeatilsAjax()
					wx.navigateBack({

					})
				}, 500)
			}
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		let orderId = options.orderId || '';
		if (options.refundInfo1) {
			var refundInfo1 = JSON.parse(options.refundInfo1);
			console.log(refundInfo1)
			var tempFilePaths = refundInfo1.return_img;
			tempFilePaths = tempFilePaths.split(',');
			this.setData({
				tempFilePaths,
				complete: true
			})

		}
		this.setData({
			orderId,
			refundInfo1
		})
		this.wayArrayAjax()
		this.reasonArrayAjax()
		this.getMoneyAjax()
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
